(() => {
  // 创建 Vue 实例并挂载到 id 为 main 的元素上
  new Vue({
    el: "#main",
    mixins: [mixin],
    data: {
      subtxt: "",
      retrospectiveDiagnosedTime:"",
      reivewList:[
        {
          reivewLabel:"抗生素给药时间",
          enName:"antibiotic_time",
          type:1
          // "retrospectiveInfoId":"int //病患回顾性信息主键ID",
          // "doctorInfoId":"int //医生信息主键ID",
          // "patientInfoId":"int //病患信息主键ID",
          // "type":"string //检查项类型 1-抗生素; 2-微生物样本",
          // "name":"string //名称",
          // "enName":"string //英文名称",
          // "checkedDate":"date //检查项检查时间",
          // "createdDate":"date",
          // "createdBy":"string",
          // "updatedDate":"date",
          // "updatedBy":"string"
        },
        {
          reivewLabel:"微生物培养时间",
          enName:"culture_time",
          type:2
        },
      ],
      antibiotic_time:"",
      culture_time:"",
      othercheck_time:"",
      RetrospectiveinformationList:[],
      toDelData:null,
      isDie:"",
      showPickerVisible:false,
      currentDate:"",
      pickerType:"",
      otherMsg:{
        Medical_Imaging:"影像检查",
        Pathogen_Blood:"病原检查",
        Smear:"涂片"
      },
      isSetTime:"1",
      // <option value="Medical_Imaging">影像检查</option>
      //             <option value="Pathogen_Blood">病原检查</option>
      //             <option value="Smear">涂片</option>
      // 时间组件初始化id数组
      // 抗生素给药时间  antibiotic_time   1-抗生素; 2-微生物样本
      // 微生物培养时间 culture_time
      // review
    },
    created () {
      toastr.options = {
        positionClass: 'toast-top-center'
      };
      // 初始化页面 需要获取当前医生信息

      this.initData()
      this.getAllPatientList()
      // this.getreivewList()
      // 页面加载完成后执行的方法
      this.getTreatmentPlanList()

    },
    methods: {
      // 显示选择器
      showPicker (type) {
        // console.log(type,this.getValueByPath(this,type),1)
        this.pickerType = type; // 设置选择器类型
        this.currentDate = this.getValueByPath(this,type) ? dayjs(this.getValueByPath(this,type)).toDate() : "" ; // 1696494645000
        this.showPickerVisible = true; // 显示选择器
      },
      onConfirm(value){
        // console.log(value)
        this.setValueByPath(this,this.pickerType,dayjs(value).format('YYYY-MM-DD HH:mm:ss'),"")
        // this[this.pickerType] = dayjs(value).format('YYYY-MM-DD HH:mm:ss') // 格式化日期
        // this.antibiotic_time =  dayjs(value).format('YYYY-MM-DD HH:mm:ss') // 格式化日期
        this.showPickerVisible = false; // 关闭选择器
      },
      // 日期组件初始化控制
      // 选择最小最大日期时间
      // 定义方法
      // 获取回顾信息列表
      getreivewList(){
        const URL = "/patient/retrospective/list?patientInfoId=" + sessionStorage.getItem("patientInfoId");
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          // body: JSON.stringify({patientInfoId:sessionStorage.getItem("patientInfoId"),type:1})
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                console.log(data, "列表")
                // this.TreatmentPlanList = data?.data ?? []
              } else {
                toastr.error(data.msg ? data.msg : "操作失败")
              }
            })
            .catch((error) => {
              console.error(error);
            });
        this.reivewList = []
      },
      // 切换患者信息
      changePatient () {
        $('#staticBackdrop').modal('show');
      },
      changePatientHander (item) {
        // 确认选择患者 更新患者信息 并通过患者id查询当前患者基础信息及病症信息
        this.getPaitentDetail(item.patientInfoId)
        $('#staticBackdrop').modal('hide');
      },
      getPaitentDetail (patientInfoId) {
        const URL = "/patient/detail?patientInfoId=" + patientInfoId;
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                // 更新一下患者id方便切换页面做 病人信息查询
                sessionStorage.setItem("patientInfoId", patientInfoId)
                this.patientInfo.patientDiseaseDetailList = []
                Object.assign(this.patientInfo, data.data)
                // 判断日期是否为1990-01-01 00:00:00  如果是则不展示
                if (this.patientInfo.retrospectiveDiagnosedTime == "1990-01-01 00:00:00") {
                  this.isSetTime = "0"
                }
                this.getTreatmentPlanList()
              } else {
                console.error(data.msg);
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      upadisDie(){
        if(this.patientInfo.isDieWithin28Days== ''){
          toastr.error("请选择选项！")
          return
        }
        const data = {
          patientInfoId: sessionStorage.getItem("patientInfoId"),
          isDieWithin28Days: this.patientInfo.isDieWithin28Days
        }
        const URL = "/patient/upadte/retrospective/isDie?patientInfoId=" + data.patientInfoId + "&retrospectiveDiagnosedTime=" + data.isDieWithin28Days;
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data)
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                this.getTreatmentPlanList()
                toastr.success(data.msg ? data.msg : "操作成功")

              } else {
                toastr.error(data.msg ? data.msg : "操作失败")
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      setTimeChange(){
        this.patientInfo.retrospectiveDiagnosedTime = ""
      },
      // 更新脓毒症新消息
      upadteretrospective () {
        // "patientInfoId":"int //病患主键【必须】",
        // "retrospectiveDiagnosedTime":"date //病患回顾性脓毒症确认时间",
        // "updatedBy":"string //更新人" this.patientInfo.diagnosedTime
        if(this.isSetTime == 1){
          if(!this.patientInfo.retrospectiveDiagnosedTime){
            toastr.error("请选择时间！")
            return
          }
          // 脓毒症更新时间必须在入院时间 前24 小时之后
          //  if(dayjs(this.patientInfo.retrospectiveDiagnosedTime).diff(dayjs(this.patientInfo.firstIcuAdmissionTime), 'minute') <60)
          // if(dayjs(this.patientInfo.admissionTime).add(24, 'hour').isAfter(dayjs(this.patientInfo.retrospectiveDiagnosedTime))){
          //   toastr.error('脓毒症确认时间必须在患者入院前24小时之后！');
          //   return
          // }
          if(dayjs(this.patientInfo.retrospectiveDiagnosedTime).diff(dayjs(this.patientInfo.firstIcuAdmissionTime), 'minute') < 60){
            toastr.error('脓毒症确认时间必须在患者入ICU 1小时之后！');
            return
          }
          // 不能超过入icu 后30天
          if(dayjs(this.patientInfo.retrospectiveDiagnosedTime).diff(dayjs(this.patientInfo.firstIcuAdmissionTime), 'day') > 30){
            toastr.error("脓毒症确认时间距离入icu时间不能超过30天！")
            return
          }
        }else {
          this.patientInfo.retrospectiveDiagnosedTime = "1990-01-01"
        }
        const data = {
          patientInfoId: sessionStorage.getItem("patientInfoId"),
          retrospectiveDiagnosedTime: dayjs(this.patientInfo.retrospectiveDiagnosedTime).format('YYYY-MM-DD HH:mm:ss')
        }
        const URL = "/patient/upadte/retrospective?patientInfoId=" + data.patientInfoId + "&retrospectiveDiagnosedTime=" + data.retrospectiveDiagnosedTime;
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data)
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                this.getTreatmentPlanList()
                toastr.success(data.msg ? data.msg : "操作成功")
                // console.log(data, "获取治疗方案列表")
                // this.TreatmentPlanList = data?.data ?? []
              } else {
                toastr.error(data.msg ? data.msg : "操作失败")
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      //  抗生素给药时间：
      subantibioticMsg(){
        if(!this.antibiotic_time){
          toastr.error("请选择抗生素给药时间！")
          return
        }
        const isAntibioticValid = dayjs(this.antibiotic_time).isAfter(dayjs(this.patientInfo.admissionTime).subtract(24, 'hour'))
        if(!isAntibioticValid){
          toastr.error('抗生素给药时间必须在患者入院前24小时之后！');
          return
        }
        if(dayjs(this.antibiotic_time).diff(dayjs(this.patientInfo.firstIcuAdmissionTime), 'day') > 30){
          toastr.error("抗生素给药时间距离患者入ICU时间不能超过30天！")
          return
        }
        // "retrospectiveInfoId":"int //病患回顾性信息主键ID",
        // "doctorInfoId":"int //医生信息主键ID【必须】",
        // "patientInfoId":"int //病患信息主键ID【必须】",
        // "type":"string //检查项类型 1-抗生素; 2-微生物样本【必须】",
        // "name":"string //名称【必须】",
        // "enName":"string //英文名称【必须】",
        // "checkedDate":"date //检查项检查时间【必须】",
        // "createdDate":"date",
        // "createdBy":"string",
        // "updatedDate":"date",
        // "updatedBy":"string"
        const data = {
          name:"抗生素给药时间",
          enName:"antibiotic_time",
          type:"1",
          doctorInfoId:this.patientInfo.doctorInfoId,
          patientInfoId: sessionStorage.getItem("patientInfoId"),
          checkedDate: dayjs(this.antibiotic_time).format('YYYY-MM-DD HH:mm:ss')
        }
        const URL = "/patient/retrospective/add";
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data)
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                this.getTreatmentPlanList()
                toastr.success(data.msg ? data.msg : "操作成功")
              } else {
                toastr.error(data.msg ? data.msg : "操作失败")
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      // 微生物样本采集时间：
      subcultureMsg(){
        if(!this.culture_time){
          toastr.error("请选择微生物样本采集时间！")
          return
        }
        const isAntibioticValid = dayjs(this.culture_time).isAfter(dayjs(this.patientInfo.admissionTime).subtract(24, 'hour'))
        if(!isAntibioticValid){
          toastr.error('微生物样本采集时间必须在患者入院前24小时之后！');
          return
        }
        if(dayjs(this.culture_time).diff(dayjs(this.patientInfo.firstIcuAdmissionTime), 'day') > 30){
          toastr.error("微生物样本采集时间距离患者入ICU时间不能超过30天！")
          return
        }
        const data = {
          name:"微生物样本采集时间",
          enName:"culture_time",
          type:"2",
          doctorInfoId:this.patientInfo.doctorInfoId,
          patientInfoId: sessionStorage.getItem("patientInfoId"),
          checkedDate: dayjs(this.culture_time).format('YYYY-MM-DD HH:mm:ss')
        }
        const URL = "/patient/retrospective/add";
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data)
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                this.getTreatmentPlanList()
                toastr.success(data.msg ? data.msg : "操作成功")
              } else {
                toastr.error(data.msg ? data.msg : "操作失败")
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      // 其他检查
      subotherMsg(){
        if(!this.patientInfo.othercheckname){
          toastr.error("请选择检查项！")
          return
        }
        if(!this.othercheck_time){
          toastr.error("请选择其他检查时间！")
          return
        }
        // if(dayjs(this.patientInfo.admissionTime).add(24, 'hour').isAfter(dayjs(this.othercheck_time))){
          const isAntibioticValid = dayjs(this.othercheck_time).isAfter(dayjs(this.patientInfo.admissionTime).subtract(24, 'hour'))
          if(!isAntibioticValid){
          toastr.error('其他检查时间必须在患者入院前24小时之后！');
          return
        }
        if(dayjs(this.othercheck_time).diff(dayjs(this.patientInfo.firstIcuAdmissionTime), 'day') > 30){
          toastr.error("其他检查时间距离患者入ICU时间不能超过30天！")
          return
        }
        const data = {
          name:this.otherMsg[this.patientInfo.othercheckname],
          enName:this.patientInfo.othercheckname,
          type:"3",
          doctorInfoId:this.patientInfo.doctorInfoId,
          patientInfoId: sessionStorage.getItem("patientInfoId"),
          checkedDate: dayjs(this.othercheck_time).format('YYYY-MM-DD HH:mm:ss')
        }
        const URL = "/patient/retrospective/add";
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data)
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                this.getTreatmentPlanList()
                toastr.success(data.msg ? data.msg : "操作成功")
              } else {
                toastr.error(data.msg ? data.msg : "操作失败")
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      // 获取列表
      getTreatmentPlanList () {
        // patientInfoId
        // warningId
        const data = {
          patientInfoId: sessionStorage.getItem("patientInfoId"),
        }
        const URL = "/patient/retrospective/list?patientInfoId=" + data.patientInfoId;
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          // body: JSON.stringify(data)
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                console.log(data, "获取回顾性列表")
                this.RetrospectiveinformationList = data?.data ?? []
              } else {
                console.error(data.msg);
              }
            })
            .catch((error) => {
              console.error(error);
            });

      },
      Retrosdel (e) {
        // 删除检查项 后刷新检查项列表
        this.toDelData = e
        $('#confirmModaldel').modal('show');

      },
      todel(){
        this.RetrosDeleHander()
      },
      RetrosDeleHander () {
        const URL = "/patient/retrospective/delete";
        fetch(apiUrl + URL, {
          method: "DELETE",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify([this.toDelData.retrospectiveInfoId])
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                $('#confirmModaldel').modal('hide');
                toastr.success(data.msg ? data.msg : "删除成功！")
                this.getTreatmentPlanList()
              } else {
                console.error(data.msg);
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      // 结束监控异步方法
      endCheck(){
        const URL = "/patient/update/finishedState";
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            patientInfoId: sessionStorage.getItem("patientInfoId"),
            isFinished:1,
            finishedTime:dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          })
        })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code === 200) {
                toastr.success(data.msg ? data.msg : "设置成功！")
                this.getAllPatientList()
              } else {
                console.error(data.msg);
                toastr.error(data.msg ? data.msg : "设置失败！")
              }
            })
            .catch((error) => {
              console.error(error);
            });
      },
      // 结束患者监控
      endPatientCheck(){
        vant.Dialog.confirm({
          title: '提示',
          message: '确认患者列表中不在显示该患者？',
        })
            .then(() => {
              this.endCheck()
            })
            .catch(() => {
            });
      }
    },
  });
})();

